package frist50;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class T0001 {
    public int[] twoSum(int[] nums, int target) {
        int[] re = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    re = new int[]{i, j};
                    break;
                }
            }
        }
        return re;
    }

    public int[] twoSum2(int[] nums, int target) {
        Set<Integer> hashset = new HashSet<>();
        int left = 0, right = 0;
        for (int i = 0; i < nums.length; i++) {
            if (hashset.contains(target - nums[i])) {
                right = i;
                break;
            } else {
                hashset.add(nums[i]);
            }
        }
        for (int i = 0; i < right; i++) {
            if (nums[i] + nums[right] == target) {
                left = i;
            }
        }
        int[] re = {left, right};
        return re;
    }

}
